﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUL;

namespace Myweb.Administrator.uc
{
    public partial class ctrAdvertise : System.Web.UI.UserControl
    {
        private AdvertiesBul bul = new AdvertiesBul();
        private void LoadData()
        {
            GridView1.DataSource = AdvertiesBul.Advertise_GetByAll("vi");
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        
        protected void ChkAll(object sender, EventArgs e)
        {
            CheckBox chkh = (CheckBox)GridView1.HeaderRow.FindControl("cbAll");
            foreach (GridViewRow row in GridView1.Rows)
            {
                var chk = (CheckBox)row.FindControl("cbitem");
                chk.Checked = chkh.Checked;
            }
           
        }
        
       
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            LoadData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            LoadData();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (GridView1.DataKeys[e.RowIndex] == null)
                return;
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            bul.Advertise_Delete(id);
            LoadData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[3].Attributes.Add("Onclick", "return confirm('Bạn xóa dữ liệu này?')");
            }
            
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (GridView1.DataKeys[e.RowIndex] == null)
                return;
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            var name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
            var image = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtimg");
            var listAdv = AdvertiesBul.Adverties_GetById(id);
            if (listAdv.Count == 0)
                return;
            var itemUpdate = listAdv[0];
            itemUpdate.name = name.Text;
            itemUpdate.image = image.Text;
            bul.Advertise_Update(itemUpdate);
            GridView1.EditIndex = -1;
            LoadData();

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            //if (FileUpload1.HasFile)
            //    try
            //    {

            //        FileUpload1.SaveAs(Server.MapPath("Images\\" + FileUpload1.FileName));
            //        //Label1.Text = "File name: " +
            //        //     FileUpload1.PostedFile.FileName + "<br>" +
            //        //FileUpload1.PostedFile.ContentLength + " kb<br>" +
            //        // "Content type: " +
            //        // FileUpload1.PostedFile.ContentType;
            //        //Label1.Text = extent;
            //    }
            //    catch (Exception ex)
            //    {

            //        Label1.Text = "ERROR: " + ex.Message;
            //    }
            //else
            //{

            //    Label1.Text = "You have not specified a file.";
            //}

            //Adverties adv = new Adverties();
            //adv.image = @"/Administrator/Images/" + FileUpload1.PostedFile.FileName; ;
            //adv.name = txtName.Text;
            //adv.height = "";
            //adv.width = "";
            //adv.position = "";
            //adv.ord = "";
            //adv.pageid = "";
            //adv.target = "";
            //adv.link = "#";
            //adv.lang = "";
            //adv.active = "";
            //adv.click = "";
            //adv.content = "";
            //bul.Advertise_Insert(adv);
            //LoadData();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           
            foreach (GridViewRow row in GridView1.Rows)
            {
                var cb = (CheckBox)row.FindControl("cbitem");
                string id = GridView1.DataKeys[row.RowIndex].Value.ToString();
                if (cb != null && cb.Checked)
                {
                    bul.Advertise_Delete(id);
                    LoadData();
                }
            }

        }
    }
}